/*
 * Copyright (c) 2010, Thiago Campos Viana.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Thiago Campos Viana nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package client;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.nio.ByteBuffer;

/**
 *
 * @author Thiago
 */
public class KeyHandler implements KeyListener {

    Game game;

    public KeyHandler(Game game) {

        this.game = game;
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        if (game.canSend) {
            game.canSend=false;
            try {


                // send request
                ByteBuffer buf = ByteBuffer.allocate(6);
                buf.put((byte)1);
                buf.putInt(game.id);
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        buf.put((byte)1);
                        break;
                    case KeyEvent.VK_RIGHT:
                        buf.put((byte)2);
                        break;
                    case KeyEvent.VK_DOWN:
                        buf.put((byte)3);
                        break;
                    case KeyEvent.VK_LEFT:
                        buf.put((byte)4);
                        break;
                    case KeyEvent.VK_1:
                        System.out.println("aqui");
                        buf = ByteBuffer.allocate(5);
                        buf.put((byte)3);
                        buf.putInt(game.id);
                        break;
                    default:
                        buf.put((byte)0);
                        break;

                }

                //buf[2] = "C".getBytes()[0];

                game.packet = new DatagramPacket(buf.array(), buf.array().length, game.address, 4445);
                game.socket.send(game.packet);
                buf = ByteBuffer.allocate(1000);
                // get response
                game.packet = new DatagramPacket(buf.array(), buf.array().length);
                game.socket.receive(game.packet);
                //System.out.println(packet.getData()[0]);


            } catch (IOException ex) {
                //Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void keyReleased(KeyEvent e) {
    }
}
